﻿<?php
class upload_model extends CI_Model{
	
	public function __construct(){
		parent::__construct();
	}
	
	public function upload_image($path, $name, $width, $height, $fieldName=null)
	{
				
		$config['upload_path'] = './uploads/images/'.$path;
		$config['allowed_types'] = 'gif|jpg|png|jpeg';
		if($name != null)
			$config['file_name'] = $name;
		$config['remove_spaces'] = true;
		$config['overwrite'] = false;
		
		$this->load->library('upload', $config);
		
		$fieldName = $fieldName?:'userfile';
		
		if (!$this->upload->do_upload($fieldName))
		{
			return false;
		}
		else
		{
			$data = $this->upload->data();
			$origWidth = $data['image_width'];
			$origHeight = $data['image_height'];
			if($origWidth!=$width || $origHeight!=$height)
			{
				$this->resize($data, $width, $height);
			}
			
			return $data['file_name'];
		}
	}
	
	public function upload_pdf($fieldName=null)
	{
		$config['upload_path'] = './uploads/pdf/';
		$config['allowed_types'] = 'pdf';
		$config['remove_spaces'] = true;
		$config['max_size']    = '0';
		
		$this->load->library('upload', $config);
		
		$fieldName = $fieldName?:'userfile';
		
		if (!$this->upload->do_upload($fieldName))
		{
			return false;
		}
		else
		{
			$data = $this->upload->data();
			
			return $data['file_name'];
		}
	}
	
	public function resize($imgData, $width, $height)
	{	
		$config['image_library'] = 'gd2';
		$config['source_image'] = $imgData['full_path'];
		$config['maintain_ratio'] = TRUE;
		$config['width'] = $width;
		$config['height'] = $height;
		
		$this->load->library('image_lib', $config);
		
		$this->image_lib->resize();
	}
}
?>